#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
功能：ssh连接工具
"""
import paramiko



class ParamikoTool(object):

    def excute(self, ip, username, sshport, password, cmd):
        ssh = paramiko.SSHClient()

        try:

            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip, sshport, username, password)
            stdin, stdout, stderr = ssh.exec_command(command=cmd)
            err = stderr.read()
            try:
                if err != '':
                    print err
                    return 'false'
                else:
                    stdread = stdout.read()
                    print 'ggg'

                    return stdread
            except Exception, e:
                return 'false'
        except Exception, e:
            return 'false'
        finally:
            ssh.close()

    def excutenoresponse(self, ip, username, sshport, password, cmd):
        ssh = paramiko.SSHClient()

        try:

            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip, sshport, username, password)
            ssh.exec_command(command=cmd)
        except Exception, e:
            return 'false'
        finally:
            ssh.close()

